<template>
  <div>
    <div style="margin-left:20px;margin-top:10px;margin-bottom:5px;">
    </div>
   <div id="data-table" style="margin-left:10%;margin-right:10%;">
    <el-form :model="userVO" label-width="180px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名：">
            <span style="width:20%;">&nbsp;</span>
            <span style="width:50%;"> {{userVO.userName}} </span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="昵称：">
            <span style="color:red;width:20%;">*</span>
            <el-input style="width:50%;" v-model="userVO.nickName" clearable></el-input>
          </el-form-item>
        </el-col>  
      </el-row>
      <el-row>
        <!-- <el-col :span="12">
          <el-form-item label="头像：">
            <el-upload
                  class="avatar-uploader"
                  action="api/file/minio/uploadPicture"
                  :headers="headerContent"
                  name="file"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
                  <img v-if="userVO.avatar" :src="userVO.avatar" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="性别：">
            <span style="color:red;width:20%;">*</span>
              <el-select clearable style="width:50%;" v-model="userVO.gender">
                  <el-option value="man" label="男"></el-option>
                  <el-option value="woman" label="女"></el-option>
              </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="邮箱：">
            <span style="width:20%;">&nbsp;</span>
            <el-input clearable style="width:50%;" v-model="userVO.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">  
          <el-form-item label="qq：">
            <span style="width:20%;">&nbsp;</span>
            <el-input style="width:50%;" v-model="userVO.qq" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="创建时间：">
            <span style="width:20%;">&nbsp;</span>
            <span style="width:50%;">{{userVO.createTime | datefmt('YYYY-MM-DD HH:mm:ss')}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最近修改密码时间：">
            <span style="width:20%;">&nbsp;</span>
            <span style="width:50%;">{{userVO.lastUpdatePassTime | datefmt('YYYY-MM-DD HH:mm:ss')}}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="更新时间：">
            <span style="width:20%;">&nbsp;</span>
            <span style="width:50%;">{{userVO.updateTime | datefmt('YYYY-MM-DD HH:mm:ss')}}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

   </div>
   <div>
       <el-row style="text-align:center;margin-bottom:10px;margin-left:20px;">
        <el-col :span="24">
            <el-button size="small" id="save-btn" plain type="primary" class="el-icon-check" @click="saveData">保存</el-button>
        </el-col>
      </el-row>
   </div>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'

export default {
  data () {
    return {
        userVO: {
            id: null,
            userName: '',
            nickName: '',
            email: '',
            avatar: '',
            gender: '',
            qq: '',
            genderName: '',
            createTime: '',
            lastUpdatePassTime: '',
            updateTime: ''
        },
        headerContent: {}
    }
  },
  methods: {
      loadData () {
          let vm = this
          vm.$http.get('api/user/info?token=' + getToken()).then(res => {
            if (res.data.code === 0){
              vm.userVO = res.data.data
            }
            else {
              vm.$message.error(res.data.msg)
            }
          })
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      handleAvatarSuccess(res, file) {
        let vm = this
        vm.userVO.avatar = res.data.url
      },
      saveData () {
          let vm = this
          vm.$http.post('api/user/update',vm.userVO).then(res => {
            if (res.data.code === 0){
                vm.$message({
                    message: '修改信息成功',
                    type: 'success'
                })
                vm.loadData()
            }
            else {
              vm.$message.error(res.data.msg)
            }
          })
      }
  },
  mounted() {
     let vm = this
     vm.loadData()
  },
  beforeMount () {
    let vm = this
    vm.headerContent = { 'Authorization': getToken() }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 78px;
    height: 78px;
    line-height: 78px;
    text-align: center;
  }
  .avatar {
    width: 78px;
    height: 78px;
    display: block;
  }
</style>
